import {Component, Input, OnInit, ViewChild,ChangeDetectionStrategy} from '@angular/core';
import {ColumnType, SPage, TablePageComponent} from "@shared/components/table-page/table-page.component";
import {NzMessageService, NzModalService,NzModalRef} from "ng-zorro-antd";
import { UploadFile } from 'ng-zorro-antd/upload';
import {AreaService} from "../../sys-setting/area-manage/area.service";
import {ApiService} from "@core/services/api";
import {HttpClient} from "@angular/common/http";
import { ChangeDetectorRef } from '@angular/core';
import { XlsxService } from '@delon/abc/xlsx';
import { NzListModule } from 'ng-zorro-antd/list';

@Component({
  selector: 'app-import-person',
  templateUrl: './import-person-face.component.html',
  styles: []
})
export class ImportPersonFaceComponent implements OnInit {

  @Input() id;
  @Input() areaRank;
  file:any;
  fileList: UploadFile[] = [];
  ds: any = [];
  showErr:boolean = false;
  loading:boolean = false
  index:number=0

  data:any;

  constructor(
    private modal: NzModalService,
    private msg: NzMessageService,
    private area: AreaService,
    private api: ApiService,
    private http: HttpClient,
    private modalRef: NzModalRef,
    private xlsx: XlsxService,
    private cdr: ChangeDetectorRef
  ) { }

  ngOnInit() {
  }
  dataProcess(){
    
  }
  // change(e): void {
  //   console.log(e)
  //     const node = e.target as HTMLInputElement;
  //     this.xlsx.import(node.files![0]).then(res => {
  //       this.data = res;
  //       console.log(this.data)
  //       this.cdr.detectChanges();
  //     });
  //     node.value = '';
  // }
  beforeUpload =  (file: UploadFile): boolean => {
    this.fileList = this.fileList.concat(file);
    return false;
  };

  resetForm():void{
    this.modalRef.destroy();
  }
  async submit():Promise<void>{
    this.index = 0
    this.ds=[]
    this.loading = true
    for(let item of this.fileList){
      this.index++
      try {
        await this.uploadFile(item)  
      } catch (error) {
        console.log(error)
      }
    }
  }
  uploadFile(file){
    return new Promise((resolve,reject)=>{
        const formData = new FormData();
        formData.append('file',file)
        this.http.post("/core/person/uploadPersonImg"+"?roomId="+this.id,formData).subscribe((res:any) => {
          if (res.status === '200') {
            resolve(res.data)
          } else {
            reject(res.data)
            this.ds.push(res.data)
            this.showErr=true
          }
          if(this.index == this.fileList.length && !this.showErr){
            this.loading = false
            //所有请求都成功完成
            this.msg.success('操作完成')
            this.modalRef.destroy()
          }else if(this.index == this.fileList.length && this.showErr){
            this.loading = false
          }
        })
    })
   
  }
}
